package com.gduf.lsm.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.gduf.lsm.entity.*;
import com.gduf.lsm.mapper.*;
import com.gduf.lsm.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author lsm
 * @Data
 * 统计数据的Service处理服务层
 */
@Service
public class GraphService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OlderMapper olderMapper;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private CulturalMapper culturalMapper;
    @Autowired
    private JoinActivityMapper joinActivityMapper;
    @Autowired
    private JoinCulturalMapper joinCulturalMapper;
    @Autowired
    private ServeMapper serveMapper;
    @Autowired
    private ApplyMaintainMapper applyMaintainMapper;
    @Autowired
    private ApplyHouseworkMapper applyHouseworkMapper;
    @Autowired
    private ApplyExamineMapper applyExamineMapper;
    @Autowired
    private ApplyMentalMapper applyMentalMapper;
    @Autowired
    private ApplyNumMapper applyNumMapper;
    @Autowired
    private FeedbackMapper feedbackMapper;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private WaterBillMapper waterBillMapper;


    /**
     * 统计用户、老人男女比例
     * @return
     */
    public List<Dict> countRole() {
        int admin=adminMapper.countAll();
        int user=userMapper.countAll();
        int older=olderMapper.countAll();

        List<Dict> dicts=CollUtil.newArrayList();
        dicts.add(Dict.create().set("name","管理员").set("value",admin));
        dicts.add(Dict.create().set("name","用户").set("value",user));
        dicts.add(Dict.create().set("name","老人").set("value",older));

        return dicts;
    }

    /**
     * 统计申请数据处理状态
     * @return
     */
    public List<Dict> countApplyNumStatus() {
        List<ApplyNum> applyNums=applyNumMapper.selectAll(null);
        Set<String> status=applyNums.stream().map(ApplyNum::getApStatus).collect(Collectors.toSet());
        List<Dict> dicts= CollUtil.newArrayList();
        for(String statu:status){
            long count=applyNums.stream().filter(applyNum -> applyNum.getApStatus().equals(statu)).count();
            Dict dict=Dict.create().set("name",statu).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 统计申请数据类型
     * @return
     */
    public List<Dict> countApplyNumType() {
        List<ApplyNum> applyNums=applyNumMapper.selectAll(null);
        Set<String> types=applyNums.stream().map(ApplyNum::getApType).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String type:types){
            long count = applyNums.stream().filter(applyNum -> applyNum.getApType().equals(type)).count();
            Dict dict=Dict.create().set("name",type).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 统计流水账各类型数量
     * @return
     */
    public List<Dict> countWaterBillType() {
        Integer userId= TokenUtils.getCurrentUser().getId();
        WaterBill wb= WaterBill.builder().userId(userId).build();
        List<WaterBill> waterBills=waterBillMapper.selectAll(wb);
        Set<String> types=waterBills.stream().map(WaterBill::getType).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String typee:types){
            long count = waterBills.stream().filter(waterBill -> waterBill.getType().equals(typee)).count();
            Dict dict=Dict.create().set("name",typee).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 统计各类型金额
     * @return
     */
    public List<Dict> countWBTypeMoney() {
        Integer userId= TokenUtils.getCurrentUser().getId();
        WaterBill wb= WaterBill.builder().userId(userId).build();
        List<WaterBill> waterBills=waterBillMapper.selectAll(wb);
        Set<String> types=waterBills.stream().map(WaterBill::getType).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String type:types){
            double sum=waterBills.stream().filter(waterBill -> waterBill.getType().equals(type)).mapToDouble(WaterBill::getMoney).sum();
            Dict dict=Dict.create().set("name",type).set("value",sum);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 统计各具体类型数量
     * @return
     */
    public List<Dict> countWBDetailType() {
        Integer userId= TokenUtils.getCurrentUser().getId();
        WaterBill wb= WaterBill.builder().userId(userId).build();
        List<WaterBill> waterBills=waterBillMapper.selectAll(wb);
        Set<String> detailTypes=waterBills.stream().map(WaterBill::getTypeDetail).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String type:detailTypes){
            long count = waterBills.stream().filter(waterBill -> waterBill.getTypeDetail().equals(type)).count();
            Dict dict=Dict.create().set("name",type).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 统计流水账各具体类型总金额
     * @return
     */
    public List<Dict> countWBDetailTypeMoney() {
        Integer userId= TokenUtils.getCurrentUser().getId();
        WaterBill wb= WaterBill.builder().userId(userId).build();
        List<WaterBill> waterBills=waterBillMapper.selectAll(wb);
        Set<String> detailTypes=waterBills.stream().map(WaterBill::getTypeDetail).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String type:detailTypes){
            double sum=waterBills.stream().filter(waterBill -> waterBill.getTypeDetail().equals(type)).mapToDouble(WaterBill::getMoney).sum();
            Dict dict=Dict.create().set("name",type).set("value",sum);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 服务平均分
     * @return
     */
    public List<Dict> countServe() {
        List<Map<String, Double>> list=serveMapper.selectGrade();
        List<Dict> dicts=CollUtil.newArrayList();
        for (Map<String,Double> map:list) {
            Dict dict=Dict.create().set("name",map.get("name")).set("value",map.get("avg_grade"));
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 养老服务状态统计
     * @return
     */
    public List<Dict> countOldServe() {
        List<ApplyMental> mentals=applyMentalMapper.selectAll(null);
        List<ApplyExamine> examines=applyExamineMapper.selectAll(null);
        Set<String> mentalsStatus=mentals.stream().map(ApplyMental::getStatus).collect(Collectors.toSet());
        Set<String> examineStatus=examines.stream().map(ApplyExamine::getStatus).collect(Collectors.toSet());
        Set<String> combine= Stream.concat(mentalsStatus.stream(),examineStatus.stream()).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for(String status:combine){
            long count=mentals.stream().filter(applyMental -> applyMental.getStatus().equals(status)).count();
            count+=examines.stream().filter(applyExamine -> applyExamine.getStatus().equals(status)).count();
            Dict dict=Dict.create().set("name",status).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 社区服务状态统计
     * @return
     */
    public List<Dict> countCommServe() {
        List<ApplyMaintain> maintains=applyMaintainMapper.selectAll(null);
        List<ApplyHousework> houseworks=applyHouseworkMapper.selectAll(null);
        Set<String> maintainStatus=maintains.stream().map(ApplyMaintain::getStatus).collect(Collectors.toSet());
        Set<String> houseworkStatus=houseworks.stream().map(ApplyHousework::getStatus).collect(Collectors.toSet());
        Set<String> combine=Stream.concat(maintainStatus.stream(),houseworkStatus.stream()).collect(Collectors.toSet());
        List<Dict> dicts=CollUtil.newArrayList();
        for (String status:combine){
            long count=maintains.stream().filter(applyMaintain -> applyMaintain.getStatus().equals(status)).count();
            count+=houseworks.stream().filter(applyHousework -> applyHousework.getStatus().equals(status)).count();
            Dict dict=Dict.create().set("name",status).set("value",count);
            dicts.add(dict);
        }
        return dicts;
    }

    /**
     * 公益活动、文化活动数量
     * @return
     */
    public List<Dict> countAllAct() {
        int act=activityMapper.countAll();
        int cul=culturalMapper.countAll();
        List<Dict> dictList=CollUtil.newArrayList();
        dictList.add(Dict.create().set("name","公益活动").set("value",act));
        dictList.add(Dict.create().set("name","文化活动").set("value",cul));
        return dictList;
    }

    /**
     * 统计公益活动状态
     * @return
     */
    public List<Dict> countAct() {
        List<JoinActivity> joinActivities=joinActivityMapper.selectAll(null);
        Set<String> status=joinActivities.stream().map(JoinActivity::getStatus).collect(Collectors.toSet());
        List<Dict> dictList=CollUtil.newArrayList();
        for (String statu:status){
            long count=joinActivities.stream().filter(joinActivity -> joinActivity.getStatus().equals(statu)).count();
            Dict dict=Dict.create().set("name",statu).set("value",count);
            dictList.add(dict);
        }
        return dictList;
    }

    /**
     * 统计文化活动状态
     * @return
     */
    public List<Dict> countCul() {
        List<JoinCultural> joinCulturals=joinCulturalMapper.selectAll(null);
        Set<String> status=joinCulturals.stream().map(JoinCultural::getStatus).collect(Collectors.toSet());
        List<Dict> dictList=CollUtil.newArrayList();
        for (String statu:status){
            long count=joinCulturals.stream().filter(joinCultural -> joinCultural.getStatus().equals(statu)).count();
            Dict dict=Dict.create().set("name",statu).set("value",count);
            dictList.add(dict);
        }
        return dictList;
    }

    /**
     * 通知对象统计
     * @return
     */
    public List<Dict> countNotice() {
        List<Notice> notices=noticeMapper.selectAll(null);
        Set<String> characters=notices.stream().map(Notice::getChara).collect(Collectors.toSet());
        List<Dict> dictList=CollUtil.newArrayList();
        for (String chara:characters){
            long count=notices.stream().filter(notice -> notice.getChara().equals(chara)).count();
            Dict dict=Dict.create().set("name",chara).set("value",count);
            dictList.add(dict);
        }
        return dictList;
    }

    /**
     * 统计咨询分类阅读量
     * @return
     */
    public List<Dict> countNews() {
        List<News> news=newsMapper.selectAll(null);
        Set<String> categorys=news.stream().map(News::getCategory).collect(Collectors.toSet());
        List<Dict> dictList=CollUtil.newArrayList();
        for (String category:categorys){
            int sum=news.stream().filter(news1 -> news1.getCategory().equals(category)).mapToInt(News::getCount).sum();
            Dict dict=Dict.create().set("name",category).set("value",sum);
            dictList.add(dict);
        }
        return dictList;
    }

    /**
     * 统计反馈状态数据
     * @return
     */
    public List<Dict> countFeedback() {
        List<Feedback> feedbacks=feedbackMapper.selectAll(null);
        Set<String> status=feedbacks.stream().map(Feedback::getStatus).collect(Collectors.toSet());
        List<Dict> dictList=CollUtil.newArrayList();
        for (String statu:status){
            long count=feedbacks.stream().filter(feedback -> feedback.getStatus().equals(statu)).count();
            Dict dict=Dict.create().set("name",statu).set("value",count);
            dictList.add(dict);
        }
        return dictList;
    }

    /**
     * 登录页面显示得分表
     * @return
     */
    /*public List<Dict> countServeLogin() {
        List<Map<String, Double>> list=serveMapper.selectGrade();
        List<Dict> dictList=CollUtil.newArrayList();
        for (Map<String,Double> map:list) {
            Dict dict=Dict.create().set("name",map.get("name"));
            dictList.add(dict);
            Dict dict1=Dict.create().set("value",map.get("avg_grade"));
        }
        return dictList;
    }*/
}
